//
//  BanBu_NavButton.m
//  BanBu
//
//  Created by banbu3 on 7/20/13.
//
//
#import "BanBu_NavButton.h"
#import "BanBu_ThemeManager.h"
#define ButtonImageNormal @"app_nav_btn_bg1.png"
#define ButtonImagePress  @"app_nav_btn_bg2.png"
#define ButtonImageBackNormal @"app_nav_back_bg1.png"
#define ButtonImageBackPress @"app_nav_back_bg2.png"

@implementation BanBu_NavButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initWithButtonType:(ButtonCreatType)buttonType
{
    if(self = [super init])
    {
        type = buttonType;
        _position = ButtonPositionNone;
        [self changeTheme];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheme) name:kThemeDidChangeNotification object:nil];
    return self;
}
-(void)changeTheme
{
    if(self)
    {
    switch (type) {
        case UIButtonCreatTypeNormal:
            [self setBackgroundImage:[[ThemeManager getThemeImage: ButtonImageNormal] stretchableImageWithLeftCapWidth:15.0 topCapHeight:15.0] forState:UIControlStateNormal];
            
//            [self setBackgroundImage:[[ThemeManager getThemeImage: ButtonImagePress] stretchableImageWithLeftCapWidth:15.0 topCapHeight:15.0] forState:UIControlStateHighlighted];
            break;
        case UIButtonCreatTypeBack:
        {
            NSString *str = [NSString stringWithFormat:@" %@",NSLocalizedString(@"returnButton", nil)];
            CGFloat btnLen = [str sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(100, 30)].width;
            
            if(IS_IOS_7)
            {
                self.frame=CGRectMake(0, 0, btnLen+20, 30);
            }
            else
            {
                self.frame=CGRectMake(6, 7, btnLen+20, 30);
            }
            
            [self setTitle:str forState:UIControlStateNormal];
//
            [self setBackgroundImage:[[ThemeManager getThemeImage:@"chatReturn.png"] stretchableImageWithLeftCapWidth:30 topCapHeight:0] forState:UIControlStateNormal];
            
//            [self setBackgroundImage:[[ThemeManager getThemeImage:@"chatReturn.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, -30, 0, 0) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
            self.titleLabel.font = [UIFont systemFontOfSize:16];
            break;

        }

        case UIButtonCreatTypeAdd:
            break;

        case UIButtonCreatTypeList:
            [self setBackgroundImage:[[ThemeManager getThemeImage:ButtonImageNormal] stretchableImageWithLeftCapWidth:15.0 topCapHeight:15.0] forState:UIControlStateNormal];
            [self setImage:[UIImage imageNamed:@"button_grid.png"] forState:UIControlStateNormal];
            break;
        case UIButtonCreatTypeChatVoice:
            [self setBackgroundImage:[ThemeManager getThemeImage:@"voicenopress.png"] forState:UIControlStateNormal];
            break;
        case UIButtonCreatTypeChatLongPress:
            [self setBackgroundImage:[ThemeManager getThemeImage:@"longvoicebtn.png"] forState:UIControlStateNormal];
            break;
        case UIButtonCreatTypeChatText:
            [self setImage:[ThemeManager getThemeImage:@"textnopress.png"]  forState:UIControlStateNormal];
            break;
        case UIButtonCreatTypeCustomBtn:
            [self setBackgroundImage:[[ThemeManager getThemeImage:@"app_btn_blue.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateNormal];
            break;
        case UIButtonCreatTypeChatRound:
            [self setBackgroundImage:[[ThemeManager getThemeImage:@"btn_plus_br.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2] forState:UIControlStateNormal];
            break;
        case UIButtonCreatTypeChatReturn:
            [self setBackgroundImage:[[ThemeManager getThemeImage:@"chatReturn.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:2] forState:UIControlStateNormal];
            break;
        default:
            break;
    }
//        [self setTitleEdgeInsets:UIEdgeInsetsMake(3, 9, 2, 2)];
    
    
    
    }
}

-(UIEdgeInsets)alignmentRectInsets
{
     UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    if (IS_IOS_7)
    {
        if(_position == ButtonPositionLeft)
        {
            insets = UIEdgeInsetsMake(0, 10.0f, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -8.0f, 0, 0);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -8.0f, 0, 0);
        }
        else if(_position == ButtonPositionRight)
        {
            insets = UIEdgeInsetsMake(0, 0, 0, 10.0f);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -8.0f);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -8.0f);
        }
    }
    return insets;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
-(void)dealloc
{
    [super dealloc];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangeNotification object:nil];
}
@end
